import { defineStore } from 'pinia'
import { isEmpty } from '@/assets/js/utils'

export const loginStore = defineStore('login', {
	state: () => {
		return {
			//账户相关权限（菜单、按钮权限等）数据
			authInfo: <any>{}
		}
	},
	getters: {},
	actions: {
		//是否登录状态
		isLogin() {
			const tokenObject = uni.getStorageSync('uniToken')
			if (tokenObject) {
				return true
			} else {
				return false
			}
		},
		//设置token
		async setLoginToken(token: string, rfToken?: string) {
			uni.setStorageSync('uniToken', token)
			if (rfToken) {
				uni.setStorageSync('uniRfToken', rfToken)
			}
		},
		//登录操作
		async login(data: any) {
			try {
				await this.setLoginToken(data.token, data.refreshToken)
				uni.reLaunch({
					url: '/pages/index/index'
				})
				return true
			} catch (e) {
				return false
			}
		},
		//退出登录
		logout() {
			uni.clearStorageSync()
			this.$reset()
		},
	}
})
